package com.jb.web.application.event;

import com.jb.framework.DataSourceManager;
import com.jb.search.CatalogCrawler;
import com.jb.search.lucene.CatalogCrawlerImpl;
import com.jb.search.meta.Catalog;
import com.jb.search.meta.CatalogReader;
import com.jb.web.application.configuration.Config;
import com.jb.web.application.configuration.DBConfig;
import com.jb.web.bean.Form;
import com.jb.web.bean.ListBean;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;
import com.jb.web.form.BeanForm;
import com.jb.web.model.ApplicationConfig;
import com.jb.web.util.FileBrowser;

import java.io.File;

import java.sql.Connection;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class ApplicationEH {
    public void onBrowsingPage(WebContext ctx, UIEvent event) {
        ListBean listBean = (ListBean) event.getSource();
        String ext = "" + listBean.getValue();
        FileBrowser fb = new FileBrowser(ctx.getApplicationTop() + "/com/jb/web/bean/Page");
        fb.stepIn("", "", ext);
        listBean.setList(fb.getFileList());
    }

    public void onCrawlEvent(WebContext ctx, UIEvent event) {
        List catalogs = new CatalogReader().read(ctx.getWebTop() + "WEB-INF/searchConfig.xml");
        String location = Config.getInstance().getSearchIndexLocation();
        Spider spider = new Spider(location, catalogs);
        spider.run();
    }

    public void onCreateDefault(WebContext ctx, UIEvent event) {
        Config.getInstance().createDefault(ctx);
    }

    public void onDeleteAppl(WebContext ctx, UIEvent event) {
        String applName = event.getParameter("applName");
        List list = ctx.getSession().getApplications();

        for (int i = 0; i < list.size(); i++) {
            ApplicationConfig appl = (ApplicationConfig) list.get(i);

            if (applName.equals(appl.getId())) {
                appl.delete(ctx);

                return;
            }
        }

        ctx.addErrorMessage(this, "Application not found " + applName);
        ctx.renderResponse();
    }

    public void onTestConnection(WebContext ctx, UIEvent event) {
        try {
            Connection conn = DataSourceManager.getConnection(this);
            DataSourceManager.releaseConnection(this, conn);
            ctx.addParameter("testMsg", "Test connection successful!");
        } catch (Throwable e) {
        //    e.printStackTrace();
            ctx.addParameter("testMsg", "Test connection failed!"+e);
//            ctx.addErrorMessage(this, "Connection test failed");
        }
    }

    public void onUpdateConfiguration(WebContext ctx, UIEvent event) {
        Form form = (Form) event.getSource();
        Config config = Config.getInstance();

        if ("dbForm".equals(form.getClientId())) {
            DBConfig dbConfig = config.getDBConfig();
            BeanForm.assignFormToBean(dbConfig, form);
        } else if ("configForm".equals(form.getClientId())) {
            BeanForm.assignFormToBean(config, form);
        }

        config.update();
    }

    static class Spider //implements Runnable 
     {
        List list = new ArrayList();
        String indexLoc;

        Spider(String indexLoc, List list) {
            this.list.addAll(list);
            this.indexLoc = indexLoc;
        }

        public void run() {
            Connection connection = null;

            try {
                connection = DataSourceManager.getConnection(this);

                for (Iterator iter = list.iterator(); iter.hasNext();) {
                    Catalog cat = (Catalog) iter.next();

                    cat.setIndexLocation(indexLoc + File.separator + "search" + File.separator + cat.getName());

                    CatalogCrawler crawler = new CatalogCrawlerImpl();

                    crawler.setCatalog(cat);
                    crawler.crawl(connection);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
               DataSourceManager.releaseConnection(this, connection);
            }
        }
    }
}
